import {Injectable} from '@angular/core';
import {SysRegionSearchForm} from '../entity/sys-region-search-form';
import {RegionLevel, ShowDeepType, SysRegion} from '../entity/sys-region';
import {BASE_HTTP_OPTIONS, baseHandleError, BaseService, ReturnForm} from '@sb/base';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {BaseTreeService} from '@sb/base/core/service/base-tree.service';
import {SysRegionTreeNode} from '../entity/sys-region-tree-node';

/**
 * 机构服务
 */
@Injectable({
    providedIn: 'root'
})
export class SysRegionTreeNodeService extends BaseTreeService<SysRegion, SysRegionSearchForm, SysRegionTreeNode> {

    readonly basePath = 'sys/region';

    constructor(private http: HttpClient) {
        super(http);
    }

    public getChildren(parentCode: string, deep?: ShowDeepType): Observable<ReturnForm<SysRegion[]>> {
        if (deep) {
            deep = 1;
        }
        return this.http.post<ReturnForm<SysRegion[]>>(`${this.url}/getChildren`, {
            parentCode: parentCode,
        }, BASE_HTTP_OPTIONS).pipe(
            catchError(baseHandleError<ReturnForm<SysRegion[]>>('post'))
        );
    }

    public getChildrenNoSpatial(parentCode: string, deep?: ShowDeepType): Observable<ReturnForm<SysRegion[]>> {
        if (deep) {
            deep = 1;
        }
        return this.http.post<ReturnForm<SysRegion[]>>(`${this.url}/getChildrenNoSpatial`, {
            parentCode: parentCode,
        }, BASE_HTTP_OPTIONS).pipe(
            catchError(baseHandleError<ReturnForm<SysRegion[]>>('post'))
        );
    }


    public getNextRegionLevel(level: RegionLevel | string): RegionLevel | null {
        if (level === 'COUNTRY') {
            return 'PROVINCE';
        } else if (level === 'PROVINCE') {
            return 'CITY';
        } else if (level === 'CITY') {
            return 'DISTRICT';
        } else if (level === 'DISTRICT') {
            return 'STREET';
        } else if (level === 'STREET') {
            return 'CUN';
        } else if (level === 'CUN') {
            return 'GROUP';
        }
        return null;
    }


    public getPrevRegionLevel(level: RegionLevel | string): RegionLevel | null {
        if (level === 'GROUP') {
            return 'CUN';
        } else if (level === 'CUN') {
            return 'STREET';
        } else if (level === 'STREET') {
            return 'DISTRICT';
        } else if (level === 'DISTRICT') {
            return 'CITY';
        } else if (level === 'CITY') {
            return 'PROVINCE';
        } else if (level === 'PROVINCE') {
            return 'COUNTRY';
        }
        return null;
    }


    commit(entity: SysRegion): Observable<ReturnForm<SysRegion>> {
        return this._http.post<ReturnForm<SysRegion>>(`${this.url}/commit`, JSON.stringify(entity), {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            }
        }).pipe(
            catchError(baseHandleError<ReturnForm<SysRegion>>('post'))
        );
    }

    importNextLevel(parentCode: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/importNextLevel`, {
                parentCode: parentCode
            }
        ).pipe(
            catchError(baseHandleError<ReturnForm<string>>('post'))
        );
    }
}
